package mn.idax.exchange.otc.util;

import com.alibaba.fastjson.JSON;
import mn.idax.exchange.auth.bean.AuthInfo;
import mn.idax.exchange.auth.util.Base64CodeUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @program: auth
 * @description: Base64
 * @author: zhuwei
 * @create: 2019/03/20 18:09
 */
public class Base64 {

    /**
     * 解析token
     *
     * @param token
     * @return boolean
     */
    public static AuthInfo decodeToken(String token) {
        AuthInfo authInfo = null;
        //非空判断
        if (StringUtils.isEmpty(token)) {
            return null;
        }

        //加载数据
        String payload = token.substring(token.indexOf(".") + 1, token.lastIndexOf("."));
        if (StringUtils.isEmpty(payload)) {
            return null;
        }

        //对token解密
        String deCodeAuthinfo = Base64CodeUtils.deCode(payload);
        if (StringUtils.isEmpty(deCodeAuthinfo)) {
            return null;
        }
        //解析对象
        authInfo = JSON.parseObject(deCodeAuthinfo, AuthInfo.class);
        if (authInfo == null) {
            return null;
        }
        return authInfo;
    }
}
